﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using JLL.China.Model;
using JLL.China.Model.DTO;
using LINQ = JLL.China.Infrastructure.Repositories.LINQ2SQL;
using System.IO;
using System.Web;
using System.Web.Configuration;

namespace JLL.China.Infrastructure.Repositories.ListingMedia
{
    public class ListingMediaRepository : SQLRepositoryBase<Model.ListingMedia, int>, IListingMediaRepository
    {
        public int CountItem { get; set; }
        public override Model.ListingMedia FindBy(int key)
        {
            var listing = DataContext.ListingMedias.SingleOrDefault(x => key.Equals(x.Media_ID));

            if (listing != null)
            {
                return ListingMediaFactory.FromLINQObject(listing);
            }
            else
            {
                return null;
            }

        }
        public override IList<Model.ListingMedia> FindAll()
        {
            throw new NotImplementedException();
        }

        protected override void PersistNewItem(Model.ListingMedia item)
        {
          
            DataContext.ListingMedias.InsertOnSubmit(ListingMediaFactory.ToLINQObject(item));

        }
        //
        public decimal FindByListingIDImageType(decimal id, string imageType)
        {
            var listing = DataContext.ListingMedias.SingleOrDefault(x => x.ListingsFID.Equals(id) && x.ImgType.Equals(imageType));

            if (listing != null)
            {
                //var res = ProjectDirectoryMediaFactory.FromLINQObject(listing);
                return listing.Media_ID;
            }
            else
            {
                return 0;
            }

        }
        //
        protected override void PersistUpdatedItem(Model.ListingMedia item)
        {

            var existItem = DataContext.ListingMedias.SingleOrDefault(x=>x.Media_ID.Equals(item.ID));
            if(existItem != null){
                var it = item;
                existItem.Filename = item.Filename;
                existItem.ListingsFID = item.ListingsFID;
                existItem.IsActive = item.IsActive;
                existItem.IsDefault = item.IsDefault;
                existItem.FileData = item.FileData;
                existItem.Orders = item.Orders;
                existItem.TypeOfUpload = existItem.TypeOfUpload;
            }
        }
        protected override void PersistDeletedItem(Model.ListingMedia item)
        {
            var media = DataContext.ListingMedias.SingleOrDefault(x => x.Media_ID.Equals(item.ID));
            if (media != null)
            {

               
                
                //Delete image
                string filenamePath = HttpContext.Current.Request.PhysicalApplicationPath+(WebConfigurationManager.AppSettings["ImgListingFolder"].ToString() + media.ListingsFID.ToString() + "\\" + media.Filename);
                
                if(File.Exists(filenamePath))
                {
                    File.Delete(filenamePath);
                    string thumbpath =  HttpContext.Current.Request.PhysicalApplicationPath+(WebConfigurationManager.AppSettings["ImgListingFolder"].ToString() + media.ListingsFID.ToString() + "\\thumb\\" + media.Filename);
                    File.Delete(thumbpath);
                }
                DataContext.ListingMedias.DeleteOnSubmit(media);
              
            }
        }


        public IList<Model.ListingMedia> FindById(int id)
        {
            var items = from m in DataContext.ListingMedias
                        where m.ListingsFID == id
                        orderby m.Orders ascending 
                        select m;
            var list = items.ToList();
            var result = new List<Model.ListingMedia>();
            foreach (var n in list)
            {
                result.Add(ListingMediaFactory.FromLINQObject(n));
            }
            this.CountItem = result.Count;
            return result;

        }
   
        public void ChangeStatus(int id, int status)
        {
        }
        public void ChangeOrder(int id1, int id2)
        {
        }
        public void UpdateDefault(int ListingID)
        {
            
        }
        public int CountListingMedia()
        {
            return 0;
        }
        public IList<Model.ListingMedia> Search(ListingMediaSearchCriteria criteria)
        {
            var items = SearchByCriteria(criteria).OrderBy(x=>x.Media_ID);
            var result = new List<Model.ListingMedia>();
            var list = items.ToList();
            foreach(var item in list){
                result.Add(ListingMediaFactory.FromLINQObject(item));
            }
            return result;
        }
        private IQueryable<LINQ2SQL.ListingMedia> SearchByCriteria(ListingMediaSearchCriteria  criteria)
        {
            var items = from m in DataContext.ListingMedias select m;
            if(criteria.ListingFID.HasValue)
            {
                items = items.Where(x => x.ListingsFID == criteria.ListingFID.Value);
            }
            if(string.IsNullOrEmpty(criteria.TypeOfUpload) == false)
            {
                items = items.Where(x=>x.TypeOfUpload.Equals(criteria.TypeOfUpload));
            }
            return items;
        }
        
    }
}
